<template>
    <course-form :errors="errors" :course-id="courseId" @complete="updateCourse"></course-form>
</template>

<script>
    import CourseForm from "./CourseForm";

    export default {
        props: {courseId: {type: Number, required: true}},
        components: {CourseForm}, data() {
            return {errors: {}}
        }, methods: {
            updateCourse(form) {
                axios.put('courses/' + this.courseId, form).then((response) => {
                    this.errors = {};
                    this.$message.success('已成功保存');
                    this.$emit('complete', response.data.data)
                }).catch((error) => {
                    switch (error.response.status) {
                        case 422://表单校验失败
                            this.errors = _.mapValues(error.response.data.errors, (item) => {
                                return item.join(' ');
                            });
                            break;
                    }
                });
            }
        }
    }
</script>

<style scoped>

</style>